package com.gilgame.bukkit;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

/**
 *
 * @author Gilgame
 * @date 27 November 2011
 */
public class CommandClearEntity
{
    private List<String> animals = new ArrayList<String>();
    private List<String> monsters = new ArrayList<String>();

    public CommandClearEntity()
    {
        animals.add("chicken");
        animals.add("cow");
        animals.add("mushroomcow");
        animals.add("pig");
        animals.add("sheep");
        animals.add("squid");
        animals.add("wolf");

        monsters.add("blaze");
        monsters.add("cavespider");
        monsters.add("creeper");
        monsters.add("enderdragon");
        monsters.add("enderman");
        monsters.add("ghast");
        monsters.add("giant");
        monsters.add("magmacube");
        monsters.add("pigzombie");
        monsters.add("silverfish");
        monsters.add("skeleton");
        monsters.add("slime");
        monsters.add("snowman");
        monsters.add("spider");
        monsters.add("zombie");
    }

    public boolean run(CommandSender sender, String[] args)
    {
        if (args.length > 2 || args.length < 1)
        {
            sender.sendMessage("Invalid arguments.");
            return false;
        }

        World world = getWorld(sender, args);
        int cleared = 0;
        String name = args[0].toLowerCase();

        if (world == null)
        {
            sender.sendMessage("World was not found. If you are running this command from the console, you must supply a world name.");
            return false;
        }

        try
        {
            List<Entity> entities = world.getEntities();

            if (name.equals("animal"))
            {
                for (Entity entity : entities)
                {
                    for (String animal : animals)
                    {
                        if (entity.getClass().getName().toLowerCase().contains(animal))
                        {
                            entity.remove();
                            cleared++;
                        }
                    }
                }
            }

            else if (name.equals("monster"))
            {
                for (Entity entity : entities)
                {
                    for (String monster : monsters)
                    {
                        if (entity.getClass().getName().toLowerCase().contains(monster))
                        {
                            entity.remove();
                            cleared++;
                        }
                    }
                }
            }

            else
            {
                for (Entity entity : entities)
                {
                    if (entity.getClass().getName().toLowerCase().contains(name))
                    {
                        entity.remove();
                        cleared++;
                    }
                }
            }
        }

        catch (Exception e)
        {
            ClearEntity.LOGGER.log(Level.SEVERE, e.toString());
            sender.sendMessage("ClearEntity encountered a problem. See the console for more information.");
            return true;
        }

        if (cleared > 0)
        {
            String message = String.format("Removed %s %s(s).", cleared, name);
            sender.sendMessage(message);
        }

        else
        {
            String message = String.format("Entity '%s' not found.", name);
            sender.sendMessage(message);
        }

        return true;
    }

    private World getWorld(CommandSender sender, String[] args)
    {
        if (args.length == 2)
        {
            return sender.getServer().getWorld(args[1]);
        }

        else
        {
            if (sender instanceof Player)
            {
                return ((Player)sender).getWorld();
            }

            else
            {
                return null;
            }
        }
    }
}
